package com.feihong.yw189.fragment.base;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.bluetooth.utils.util.ToastUtils;
import com.feihong.yw189.utils.CustomServiceUtil;
import com.feihong.yw189.utils.ScreenUtil;
import com.meiqia.meiqiasdk.imageloader.MQImage;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.pedant.SweetAlert.SweetAlertDialog;
import me.yokeyword.fragmentation.SupportFragment;
import txgw.feihong.com.txgwlibrary.mqglide_loader.MQGlideImageLoader4;

/**
 * Created by Sabirjan on 2017/11/13.
 * DX189.
 * 飞宏电子
 */

public abstract class BaseFragment extends SupportFragment{
    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected boolean isLoad = false;

    public String tag;
    public static final String TAG = "营维189";
    public View fragmentView = null;

    /**
     * 是否需要界面转移动画
     */
    protected boolean isTransAnim = true;
    public Map<String,List<Callback.Cancelable>> mFragmentRequestMap;

    /**
     * 状态栏字体颜色是否为深色
     */
    public boolean isDrakFont = true;
    public int mStatusColor = Color.TRANSPARENT;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setTheme();
        mFragmentRequestMap = new HashMap<>();
    }

    public View findView(int id) {
        if (fragmentView == null) {
            return null;
        }
        return fragmentView.findViewById(id);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected abstract void setTheme();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isInit = true;
        if(view != null){
            // 跳转客服
            if(view.findViewById(R.id.img_btn_servers) != null){
                view.findViewById(R.id.img_btn_servers).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CustomServiceUtil mCustomServiceUtil = new CustomServiceUtil();
                        MQImage.setImageLoader(new MQGlideImageLoader4());
                        Intent cunstomServiceIntent = new MQIntentBuilder(getActivity())
                                .setClientInfo(mCustomServiceUtil.queryConfig())
                                .build();
                        startActivity(cunstomServiceIntent);
                    }
                });
            }
            // 跳转客服
            if(view.findViewById(R.id.iv_lin) != null){
                view.findViewById(R.id.iv_lin).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CustomServiceUtil mCustomServiceUtil = new CustomServiceUtil();
                        MQImage.setImageLoader(new MQGlideImageLoader4());
                        Intent cunstomServiceIntent = new MQIntentBuilder(getActivity())
                            .setClientInfo(mCustomServiceUtil.queryConfig())
                            .build();
                        cunstomServiceIntent.putExtra("","");
                        startActivity(cunstomServiceIntent);
                    }
                });
            }
        }
    }

    public void setPageTitle(String title){
        TextView titleTv = fragmentView.findViewById(R.id.txt_title);
        if(!TextUtils.isEmpty(title) && titleTv != null){
            titleTv.setText(title);
        }
    }
    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        ScreenUtil.setStatusBarDarkFont(_mActivity,isDrakFont);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (isTransAnim) {
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }

    /**
     * 绑定View
     */
    public abstract void initView();

    /**
     * 绑定数据
     */
    public abstract void bindData();

    public void showSystemToast(String message) {
        ToastUtils.showShortToast(getActivity(), message);
    }

    /**
     * 显示自定义Toast
     *
     * @param message
     */
    public void showToast(String message) {
        showInfoToast(message);
    }

    public void showInfoToast(String message) {
        ToastUtils.showShortToast(getActivity(), message);
    }
    public void showErrorToast(String message) {
        ToastUtils.showShortToast(getActivity(), message);
    }

    /**
     * 取消请求
     * @param clzName
     */
    public void clearRequest(String clzName){
        if(mFragmentRequestMap.get(clzName) != null){
            for(Callback.Cancelable cancelable : mFragmentRequestMap.get(clzName)){
                if(cancelable != null && !cancelable.isCancelled()){
                    cancelable.cancel();
                }
            }
        }
        mFragmentRequestMap.remove(clzName);
    }
    @Override
    public void onDestroyView() {
        isInit = false;
        isLoad = false;
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

    public void showErrorDialog(String title, String message, DialogInterface.OnDismissListener dismissListener) {
        showDialog(title, message, dismissListener, SweetAlertDialog.ERROR_TYPE);
    }

    /**
     * 成功提示对话框
     *
     * @param title           标题
     * @param message         内容
     * @param dismissListener 对话框关闭监听器
     */
    public void showDialog(String title, String message, DialogInterface.OnDismissListener dismissListener, int type) {
        try {
            SweetAlertDialog dialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.CUSTOM_IMAGE_TYPE);
            dialog.setTitleText(title);
            dialog.setContentText(message);
            dialog.setCancelable(false);
            dialog.setNeutralText("知道了");
            if (dismissListener != null) {
                dialog.setOnDismissListener(dismissListener);
            }
            dialog.show();
        } catch (Exception ex) {

        }
    }

    /**
     * 界面是否销毁
     * @return
     */
    public boolean fragmentIsOnDestory() {
        if (getActivity() == null) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (getActivity().isDestroyed()) {
                return true;
            }
        } else {
            if (getActivity().getSupportFragmentManager() == null || getActivity().getSupportFragmentManager().isDestroyed()) {
                return true;
            }
        }
        return false;
    }

}
